package com.jd.jr.util;

import com.ibatis.sqlmap.engine.type.BaseTypeHandler;
import com.jd.jr.model.RefType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


// 继承自BaseTypeHandler<Object> 使用Object是为了让JsonUtil可以处理任意类型
public class EnumTypeHandler extends BaseTypeHandler {
    @Override
    public void setParameter(PreparedStatement ps, int i, Object parameter, String jdbcType) throws SQLException {
        ps.setInt(i, ((RefType)parameter).value());
    }

    @Override
    public RefType getResult(ResultSet rs, String columnName) throws SQLException {
        return RefType.of(rs.getInt(columnName));
    }

    @Override
    public RefType getResult(ResultSet rs, int columnIndex) throws SQLException {
        return RefType.of(rs.getInt(columnIndex));
    }

    @Override
    public RefType getResult(CallableStatement cs, int columnIndex) throws SQLException {
        return  RefType.of(cs.getInt(columnIndex));
    }

    @Override
    public RefType valueOf(String s) {
        return RefType.of(Integer.parseInt(s));
    }
    

}